﻿//using System;
//using System.Collections.Generic;
//using System.Text;
//using ITAP.Core.Modelling.Primitives;
//using ITAP.ShprossesBuilder.Calculations;

//namespace ITAP.ShprossesBuilder.Modelling.Components.Constraints
//{
//    [Serializable]
//    public class ComponentCreateStickConnector : IConstraint
//    {
//        private bool enabled = true;
//        private List<Type> typeOfConnectedComponents;
//        public ComponentCreateStickConnector(Type[] typeOfConnectedComponents)
//        {
//            this.typeOfConnectedComponents = new List<Type>(typeOfConnectedComponents);
//        }
//        #region IConstraint Members

//        public bool Check(IComponent component)
//        {
//            if (!(component is IEndedComponent))
//                return true;

//            return false;

//        }

//        public void Fix(IComponent component)
//        {
//            if (!enabled) 
//                return;
//            if (!(component is IEndedComponent))
//                return;

//            IEndedComponent endedComponent = component as IEndedComponent;

//            List<IEndedComponent> endedComponents = new List<IEndedComponent>();
//            List<IComponent> connectedComponents = new List<IComponent>();
//            List<EndType> endsToConnect = new List<EndType>();

//            foreach (IComponent connectedComponent in component.ConnectedComponents)
//            {
//                if (!this.typeOfConnectedComponents.Contains(connectedComponent.GetType()))
//                    continue;

//                if (endedComponent.StartConnectedComponent == connectedComponent)
//                {
//                    if ((connectedComponent as IEndedComponent).IsConnectedToEnd(endedComponent) != EndType.NotDefined)
//                        continue;
//                    endedComponents.Add(endedComponent);
//                    connectedComponents.Add(connectedComponent);
//                    endsToConnect.Add(EndType.Start);
//                }

//                else if (endedComponent.EndConnectedComponent == connectedComponent)
//                {
//                    if ((connectedComponent as IEndedComponent).IsConnectedToEnd(endedComponent) != EndType.NotDefined)
//                        continue;

//                    endedComponents.Add(endedComponent);
//                    connectedComponents.Add(connectedComponent);
//                    endsToConnect.Add(EndType.End);
//                }
//                else
//                    continue;
//            }

//            for (int i = 0; i < endedComponents.Count; i++)
//            {
//                bool isSucceed = ConnectionProvider.Disconnect(endedComponents[i], connectedComponents[i]);

//                if (!isSucceed)
//                    continue;

//                StickConnector stickConnector = ComponentFactory.CreateStickConnector(2, 10, StickConnectorType.Virtual, component.UniqueId, component.AcceptedUniqueIdsToConnect.ToArray());
//                stickConnector.UniqueIdsEnabled = endedComponent.UniqueIdsEnabled;
//                endedComponents[i].AddInnerComponent(stickConnector);
//                stickConnector.Parent = endedComponents[i];
//                if (endsToConnect[i] == EndType.Start)
//                {
//                    stickConnector.MoveTo(endedComponents[i].StartPoint);
//                    ModelVector tangent = endedComponents[i].Primitives[0].GetTangent(0);
//                    ModelVector connectorTangent = stickConnector.Primitives[0].GetTangent(0);
//                    Angle angle = ModelCalculations.GetAngleBetweenVectors(connectorTangent, tangent, false);
//                    stickConnector.Rotate(angle.Value, stickConnector.StartPoint);
//                }
//                else if (endsToConnect[i] == EndType.End)
//                {
//                    stickConnector.MoveTo(endedComponents[i].EndPoint);
//                    ModelVector tangent = endedComponents[i].Primitives[0].GetTangent(1).Rotate(new Angle(Math.PI));
//                    ModelVector connectorTangent = stickConnector.Primitives[0].GetTangent(0);
//                    Angle angle = ModelCalculations.GetAngleBetweenVectors(connectorTangent, tangent, false);
//                    stickConnector.Rotate(angle.Value, stickConnector.StartPoint);
//                }
                        
//                bool isSuccess = ConnectionProvider.Connect(endedComponents[i], stickConnector, endsToConnect[i], EndType.End);
//                if (!isSuccess)
//                    throw new Exception();
//                isSuccess = ConnectionProvider.Connect(stickConnector, connectedComponents[i], EndType.Start);
//                if (!isSuccess)
//                    throw new Exception();

//            }
//        }

//        public bool Enabled
//        {
//            get
//            {
//                return enabled;
//            }
//            set
//            {
//                enabled = value;
//            }
//        }

//        public ConstraintAction Action
//        {
//            get { return ConstraintAction.Fix; }
//        }

//        #endregion

//        #region ICloneable Members

//        public object Clone()
//        {
//            ComponentCreateStickConnector clone = new ComponentCreateStickConnector(this.typeOfConnectedComponents.ToArray());
//            clone.enabled = this.enabled;
//            return clone;
//        }

//        #endregion
//    }
//}
